package com.blankj.utilcode.util

import android.content.Context
import android.os.Build
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.LayoutRes
import java.util.*

object LayoutUtils {
    /**
     * @param runnable The runnable
     */
    fun runOnUiThread(runnable: Runnable?) {
        UtilsBridge.runOnUiThread(runnable)
    }

    /**
     * @param runnable    The runnable.
     * @param delayMillis The delay (in milliseconds) until the Runnable will be executed.
     */
    fun runOnUiThreadDelayed(runnable: Runnable?, delayMillis: Long) {
        UtilsBridge.runOnUiThreadDelayed(runnable, delayMillis)
    }

    /**
     * Return whether horizontal layout direction of views are from Right to Left.
     *
     * @return `true`: yes<br></br>`false`: no
     */
    fun isLayoutRtl(): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            val primaryLocale: Locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Utils.getApp().resources.configuration.locales[0]
            } else {
                Utils.getApp().resources.configuration.locale
            }
            return TextUtils.getLayoutDirectionFromLocale(primaryLocale) == View.LAYOUT_DIRECTION_RTL
        }
        return false
    }



    /**
     * 布局inflate为View
     */
    fun layoutId2View(@LayoutRes layoutId: Int): View? {
        val inflate = Utils.getApp().getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        return inflate.inflate(layoutId, null)
    }
}